// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ObjectPoseEstimate.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Vision.ObjectDetection
{
    using System;
    using System.Runtime.Serialization;
    using System.Windows;

    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.PhysicalObjects;

    /// <summary>
    /// Represents an object pose estimate from IR and depth images
    /// </summary>
    [DataContract]
    public class ObjectPoseEstimate : IComparable<ObjectPoseEstimate>
    {
        /// <summary>
        /// Pose estimate in camera pixel coordinates
        /// </summary>
        [DataMember]
        public readonly Pose CameraPixelPose;

        /// <summary>
        /// Object pose after transformed to global coordinates. (This object does not do the transformation)
        /// </summary>
        [DataMember]
        public readonly Pose GlobalPose;

        /// <summary>
        /// Confidence score
        /// </summary>
        [DataMember]
        public readonly double Confidence;

        /// <summary>
        /// Confidence threshold for object
        /// </summary>
        [DataMember]
        public readonly double ConfidenceThreshold;

        /// <summary>
        /// Gets a description of the detected object
        /// </summary>
        [DataMember]
        public readonly PhysicalObjectDescription ManipulatedObjectDescription;

        /// <summary>
        /// Time in mSec to compute estimate this pose
        /// </summary>
        [DataMember]
        public readonly long ComputeTimeMsec;

        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectPoseEstimate"/> class.        
        /// Note global pose is not initialized.
        /// </summary>
        /// <param name="cameraPixelPose">Object pose in camera reference frame</param>
        /// <param name="confidence">Confidence score estimate</param>
        /// <param name="confidenceThreshold">Confidence threshold for object</param>
        /// <param name="manipulatedObjectDescription">Detected object description</param>
        /// <param name="computeTime">Elapsed time for the estimate in mSec</param>
        public ObjectPoseEstimate(
            Pose cameraPixelPose, 
            double confidence, 
            double confidenceThreshold, 
            PhysicalObjectDescription manipulatedObjectDescription = null, 
            long computeTime = 0)
        {
            this.CameraPixelPose = cameraPixelPose;
            this.Confidence = confidence;
            this.ConfidenceThreshold = confidenceThreshold;
            this.ManipulatedObjectDescription = manipulatedObjectDescription;
            this.ComputeTimeMsec = computeTime;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectPoseEstimate"/> class by updating
        /// from an existing estimate and providing the global pose
        /// </summary>
        /// <param name="other">Current estimate</param>
        /// <param name="globalPose">Object pose estimate in world reference frame</param>
        public ObjectPoseEstimate(ObjectPoseEstimate other, Pose globalPose)
        {
            this.CameraPixelPose = other.CameraPixelPose;
            this.Confidence = other.Confidence;
            this.ConfidenceThreshold = other.ConfidenceThreshold;
            this.ManipulatedObjectDescription = other.ManipulatedObjectDescription;
            this.ComputeTimeMsec = other.ComputeTimeMsec;
            this.GlobalPose = globalPose;
        }

        /// <summary>
        /// Comparator for sorting poses by confidence
        /// </summary>
        /// <param name="other">Other object to compare</param>
        /// <returns>AOne of the 3 values {-1, 0, 1} indicating if this object precedes, in same place or follows other object</returns>
        public int CompareTo(ObjectPoseEstimate other)
        {
            double diff = this.Confidence - other.Confidence;
            if (diff > MathConstants.ErrorEpsilon)
            {
                return -1;
            }
            else if (diff < MathConstants.ErrorEpsilon)
            {
                return 1;
            }

            return 0;
        }
    }
}
